package com.kehutong.payfees.service.treasure;

//import com.kehutong.common.urlconfig.FileUrlConfig;
//import com.kehutong.payfees.util.ESignTreasureUrlConfig;
//import org.coraframework.http.HttpClient;
//import org.coraframework.inject.Inject;
//import org.coraframework.json.JSON;
//import org.coraframework.json.JSONObject;
//import org.coraframework.mvc.webservice.WebService;
//import org.coraframework.util.Objects;
//
//import java.io.BufferedInputStream;
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.OutputStream;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.net.URLConnection;
//import java.util.HashMap;
//import java.util.Map;
//
//@WebService("/sign/treasure/oauth")
//public class ESignTreasureOAuthServiceImpl {
//
//    @Inject
//    private ESignTreasureUrlConfig eSignTreasureUrlConfig;
//
//    @Inject
//    private FileUrlConfig fileUrlConfig;
//
//    public JSONObject getESignTreasureToken() throws Exception {
//        JSONObject jsonObject = eSignTreasureUrlConfig.getESignTreasure("/v1/oauth2/access_token", (http) -> {
//            http.addParam("appId", eSignTreasureUrlConfig.appId);
//            http.addParam("secret", eSignTreasureUrlConfig.appSecret);
//            http.addParam("grantType", "client_credentials");
//        });
//
//        return jsonObject;
//    }
//
//    public void setRequestHeaderValue(HttpClient http) throws Exception {
//        JSONObject resultData = getESignTreasureToken();
//        String token = resultData.getString("token");
//
//        Map<String, String> header = new HashMap<>();
//        header.put("X-Tsign-Open-App-Id", eSignTreasureUrlConfig.appId);
//        header.put("X-Tsign-Open-Token", token);
//        header.put("Content-Type", "application/json; charset=UTF-8");
//
//        http.setHeaders(header);
//    }
//
//    public void setRequestHeaderValue(HttpClient http, String contentMd5) throws Exception {
//        JSONObject resultData = getESignTreasureToken();
//        String token = resultData.getString("token");
//
//        Map<String, String> header = new HashMap<>();
//        header.put("X-Tsign-Open-App-Id", eSignTreasureUrlConfig.appId);
//        header.put("X-Tsign-Open-Token", token);
//        if (Objects.nonEmpty(contentMd5)) {
//            header.put("Content-MD5", contentMd5);
//            header.put("Content-Type", "application/octet-stream");
//        }
//
//        http.setHeaders(header);
//    }
//
//    public String getFileId(String filePath, String fileName) throws Exception {
//        File file = downloadFile(filePath, fileName);
//
//        String sendResult = fileUrlConfig.sendFile("/file/upload", file);
//        JSONObject sendResultJson = JSON.parseObject(sendResult);
//        file.delete();
//        if (sendResultJson.getBoolean("isOk")) {
//            return sendResultJson.getJSONObject("data").getString("id");
//        }
//        return "";
//    }
//
//
//    private File downloadFile(String urlPath, String fileName) {
//        File file = null;
//        try {
//            // 统一资源
//            URL url = new URL(urlPath);
//            // 连接类的父类，抽象类
//            URLConnection urlConnection = url.openConnection();
//            // http的连接类
//            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
//            //设置超时
//            httpURLConnection.setConnectTimeout(1000*5);
//            //设置请求方式，默认是GET
////            httpURLConnection.setRequestMethod("POST");
//            // 设置字符编码
//            httpURLConnection.setRequestProperty("Charset", "UTF-8");
//            // 打开到此 URL引用的资源的通信链接（如果尚未建立这样的连接）。
//            httpURLConnection.connect();
//            // 文件大小
//            int fileLength = httpURLConnection.getContentLength();
//
//            // 建立链接从请求中获取数据
//            BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());
//            file = new File(System.getProperty("java.io.tmpdir") + File.separator + fileName);
//            // 校验文件夹目录是否存在，不存在就创建一个目录
//            if (!file.getParentFile().exists()) {
//                file.getParentFile().mkdirs();
//            }
//
//            OutputStream out = new FileOutputStream(file);
//            int size = 0;
//            int len = 0;
//            byte[] buf = new byte[2048];
//            while ((size = bin.read(buf)) != -1) {
//                len += size;
//                out.write(buf, 0, size);
//            }
//            // 关闭资源
//            bin.close();
//            out.close();
////           logger.debug("文件下载成功！");
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            return file;
//        }
//    }
//}
